Skip to content

[PB-6472]: feat/track-user-mail-usage#220

Open
jzunigax2 wants to merge 2 commits into
feat/mail-bucket-provisioningfrom
feat/track-user-mail-usage
Open

[PB-6472]: feat/track-user-mail-usage#220
jzunigax2 wants to merge 2 commits into
feat/mail-bucket-provisioningfrom
feat/track-user-mail-usage

Conversation

@jzunigax2

@jzunigax2 jzunigax2 commented Jun 3, 2026

Copy link
Copy Markdown

What

Adds a usedSpaceBytes field to buckets and a gateway endpoint for setting it: PUT /v2/gateway/users/:uuid/buckets/:id/usage

Mail provisions one bucket per mail account #221 and periodically reports that account's usage from Stalwart here.

Why

Bridge needs to track each user's mail storage usage next to their Drive usage so the two can be checked against a single quota. Per review feedback: the bucket is the right entity to carry used space. Per review feedback: the bucket is the right entity to carry used space

How

  • usedSpaceBytes added to the bucket domain type and mongoose schema (default 0).
  • BucketsRepository.setUsedSpaceBytes(bucketId, userId, value): a single atomic updateOne filtered on _id + owner uuid; reports whether a document matched.
  • UsersUsecase.setBucketUsage: throws BucketNotFoundError when nothing matched
  • Gateway controller validates the bucket id shape (24-hex ObjectId) and that usedSpaceBytes is a non-negative finite number; JWT-protected like the rest of the gateway.

@jzunigax2 jzunigax2 added the enhancement New feature or request label Jun 3, 2026
@jzunigax2 jzunigax2 self-assigned this Jun 3, 2026
@jzunigax2 jzunigax2 changed the title feat/track-user-mail-usage [PB-6472]: feat/track-user-mail-usage Jun 5, 2026
@sg-gs

sg-gs commented Jun 5, 2026

Copy link
Copy Markdown
Member

The totalUsedSpaceBytes was a convenience field as an aggregation of everything. I find buckets the entity more suitable to have the same field or a similar one and relate all the content generated by mail to a specific bucket in the network, the same way we do with Drive and Backups' content.

This makes the network less coupled to mail/drive/any product we may release in the future that consumes space while gaining standarisation of the calculus of the used space on a bucket. WDYT @apsantiso @jzunigax2 ?

This may be even useful for Photos or the same Drive in any foreseeable future.

Add a usedSpaceBytes field to buckets and a gateway endpoint to set it, so products whose content lives outside the network (mail/Stalwart) can report the space they consume against a network bucket.
@jzunigax2 jzunigax2 force-pushed the feat/track-user-mail-usage branch from 02b5882 to 6f17e6b Compare June 9, 2026 20:03
@jzunigax2 jzunigax2 changed the base branch from master to feat/mail-bucket-provisioning June 9, 2026 20:04
Add a new method to sum used space bytes in MongoDBBucketsRepository and update the UsersUsecase to return a UserSpaceSnapshot when setting bucket usage. Update HTTPGatewayController to send the snapshot in the response. Include tests to verify the new functionality.
@jzunigax2 jzunigax2 marked this pull request as ready for review June 11, 2026 03:38
@jzunigax2 jzunigax2 requested a review from sg-gs as a code owner June 11, 2026 03:38
Comment thread lib/core/users/usecase.ts

return {
maxSpaceBytes: user.maxSpaceBytes,
totalUsedSpaceBytes: user.totalUsedSpaceBytes + bucketsUsedSpaceBytes,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? The user's totalUsedSpaceBytes already includes the bucketsUsedSpaceBytes

@jzunigax2 jzunigax2 Jun 11, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the main issue here. The network needs to know how much space is consumed by mail. However mail blobs (bodies and attachments) are managed and stored by stalwart that upload path does not come through the network. I though about leveraging sieve scripts, event hooks or similar to also store the metadata on here, though that would create the problem of mantaining both

as it stands mail just reports the its usage which would not be part of totalUsedSpaceBytes as its contents did not go through the normal upload flow

}
}

async setBucketUsage(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know if it fits you but, isn't it better to set this on any upload so when you use the upload endpoint for mail, the computation for that bucket is set?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request ready-for-preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants